package com.hk.service.business;

import cn.hutool.core.annotation.AnnotationUtil;
import cn.hutool.core.map.MapUtil;
import com.hk.annotation.GetMessage;
import com.hk.domain.entity.Student;
import com.hk.enums.SexEnum;
import com.hk.service.business.message.AbstractMessageBusiness;
import com.hk.utils.SpringContextUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author: dpb
 * @Description TODO
 * @date: 2022/12/8 13:36
 * @Version 1.0
 */
@Service
@Slf4j
public class BatchDeclarationTypeBusiness {

    @Autowired
    private ApplicationContext applicationContext;

    @Autowired
    private MessageBusinessFactory messageBusinessFactory;

    public void processEnum(Integer sex) {
        switch (SexEnum.getSexByCode(sex)) {
            case MALE:
                log.info("=========男===========");
                break;
            case FEMALE:
                log.info("=========女===========");
                break;
            default:
                log.info("=========默认===========");
                break;
        }

    }

    public void exceptionDemo() {
        try {
            Student stu = new Student();
            stu.setAge(null);
            String age = stu.getAge();
            String[] split = age.split(",");
        } catch (Exception e) {
//            log.error("报错了，原因：{}",e.getMessage());
            //注意如果捕获到报错，直接在打日志的时候把全部栈帧打出来，e.getMessage()只能打出具体的原因，不利于错误排查
            log.error("报错了，原因：{}", e);
        }
    }

    /**
     * 获取抽象类的所有实现类并执行方法
     * @throws Throwable
     */
    public void testFactory() throws Throwable {
        Map<String, AbstractMessageBusiness> beans = applicationContext.getBeansOfType(AbstractMessageBusiness.class);
        if (MapUtil.isNotEmpty(beans)) {
            beans.keySet().stream().forEach(item -> {
                AbstractMessageBusiness messageBusiness = (AbstractMessageBusiness) SpringContextUtil.getBean(item);
                log.info("获取到的AbstractMessageBusiness的实现为是：{}, 打印的信息为：{}",messageBusiness, messageBusiness.getMsg());
                GetMessage annotation = messageBusiness.getClass().getAnnotation(GetMessage.class);
                GetMessage annotation1 = AnnotationUtil.getAnnotation(messageBusiness.getClass(), GetMessage.class);
                log.info("annotaiton1 get value~~~~{}", annotation1.msg());
                if (!Objects.isNull(annotation)) {
                    String msg = annotation.msg();
                    log.info("获取到的的注解的值为：{}",msg);
                }
            });
        }
    }

    public void testFactory2() {
//        Object obje = new Object();
//        obje = null;
//        Assert.notNull(obje,"null");
        AbstractMessageBusiness business = messageBusinessFactory.fetchService("dev");
        log.info("获取到的对象为：{}, 执行结果为：{}",business, business.getMsg());
    }
}
